package com.coding.springboot.demo.controller;

import com.coding.springboot.demo.entity.TUser;
import com.coding.springboot.demo.entity.Taccount;
import com.coding.springboot.demo.service.TUserService;
import com.coding.springboot.demo.service.TaccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * Created by qince on 2015/7/3.
 */
@Controller
@RequestMapping(value = "/manage/user")
public class TUserController {
    public static final Logger logger = LoggerFactory.getLogger(TUserController.class);
    @Autowired
    private TUserService userService;
    @Autowired
    private TaccountService taccountService;

    @RequestMapping(value = "/addUser")
    public String addUser() {
        logger.info("进入添加用户信息页面");
        return "/user/add";
    }

    @RequestMapping(value = "/saveUser",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String saveUser(@ModelAttribute TUser user) {
        logger.debug("保存用户信息：{}",user);

        userService.save(user);

        return "redirect:/manage/user/list";
    }

    @RequestMapping(value = "/list")
    public String list(Model model) {
        Iterable<TUser> users = this.userService.findAll();
        model.addAttribute("users",users.iterator());
        return "/user/list";
    }

    @RequestMapping(value = "/view/{id}",produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public TUser view(@PathVariable String id) {
        TUser user = userService.findById(id);
        return user;
    }

    @RequestMapping(value = "/redis")
    @ResponseBody
    public String redisTest() {
        System.out.println("第一次查询");
        Taccount taccount = this.taccountService.findByName("test");
        System.out.println("查到的数据：" + taccount.toString());

        System.out.println("第二次查询");
        taccount = this.taccountService.findByName("test");
        System.out.println("查到的数据：" + taccount.toString());
        return "ok";
    }
}
